﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using LeoK.Common;
using LeoK.TM.Entity;
using Telerik.Windows.Controls;

namespace LeoK.TM
{
    /// <summary>
    /// QuickTaskWindow.xaml 的交互逻辑
    /// </summary>
    public partial class QuickTaskWindow : Window
    {
        private ObservableCollection<TM_Task> _tasks;
        private bool _isPlay;
        private TM_TimeLine _curTimeLine;
        DispatcherTimer _timer = new DispatcherTimer();

        public QuickTaskWindow()
        {
            InitializeComponent();

            ShowInTaskbar = false;
            Topmost = true;
            Top = SystemParameters.PrimaryScreenHeight - 430;
            Left = SystemParameters.PrimaryScreenWidth - 400;
            this.Deactivated += (sender, e) =>
            {
                Hide();
            }; 
        }

        public async void LoadData()
        {
            using (MySqlDB db = new MySqlDB())
            {
                _tasks = new ObservableCollection<TM_Task>(await db.QueryObjs<TM_Task>("SELECT * FROM TM_Task WHERE IsFinish = 0"));
                lstBox.ItemsSource = _tasks;
            }
        }

        private async void ImgPlay_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!_isPlay)
            {
                if (lstBox.SelectedItem == null) return;

                radTab.IsEnabled = false;
                _curTimeLine = new TM_TimeLine();
                _curTimeLine.TID = (lstBox.SelectedItem as TM_Task).ID;
                _curTimeLine.StartTime = DateTime.Now;

                imgPlay.Source = new BitmapImage(new Uri("../Resources/btnPause.ico", UriKind.Relative));
                _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromSeconds(1);
                _timer.Tick += (o, args) =>
                {
                    _curTimeLine.EndTime = DateTime.Now;
                    TimeSpan span = _curTimeLine.EndTime - _curTimeLine.StartTime;
                    tbTimeSpan.Text = span.Hours.ToString("00") + ":" + span.Minutes.ToString("00") + ":" + span.Seconds.ToString("00");
                };
                _timer.Start();
                _isPlay = true;
            }
            else
            {
                radTab.IsEnabled = true;
                if ((_curTimeLine.EndTime - _curTimeLine.StartTime).TotalMinutes < 5)
                {
                    RadWindow.Alert("小于5分钟不予提交");
                }
                else
                {
                    using (MySqlDB db = new MySqlDB())
                    {
                        _curTimeLine.TimeSpan = (int)(_curTimeLine.EndTime - _curTimeLine.StartTime).TotalMinutes;
                        await db.InsertObj(_curTimeLine);
                        _curTimeLine = null;
                    }
                }

                imgPlay.Source = new BitmapImage(new Uri("../Resources/btnPlay.ico", UriKind.Relative));
                tbTimeSpan.Text = "休息中";
                _timer.Stop();
                _timer = null;
                _isPlay = false;
            }
        }

        private async void TbTask_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                using (MySqlDB db = new MySqlDB())
                {
                    TM_Task newTask = new TM_Task();
                    newTask.Name = tbTask.Text;
                    newTask.ID = await db.InsertObj(newTask);
                    tbTask.Text = "";
                    _tasks.Add(newTask);
                }
            }
        }
    }
}
